<h1 class="title-h1">订单记录</h1>
<div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">时间范围</label>
            <div class="layui-input-inline">
                <input type="text" name="date" id="date" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">快递分类</label>
            <div class="layui-input-inline">
                <select name="express">
                    <option value=""></option>
                    <option value="顺丰">顺丰</option>
                    <option value="普通快递">普通快递</option>
                </select>
            </div>
        </div> 
        <div class="layui-inline">
                <button class="layui-btn layuiadmin-btn-list" lay-submit="" id="search" lay-filter="search">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 搜索
                </button>
                <button class="layui-btn layui-btn-normal layuiadmin-btn-list" lay-submit="" id="export" lay-filter="export">
                    <i class="layui-icon layui-icon-export  layuiadmin-button-btn"></i> 导出
                </button>
        </div>
    </div>
</div>
<style>
    .layui-tab-title .layui-this{
        font-weight: bold
    }
    .green{
        color:#5fb878;
        font-weight: bold
    }
    .laytable-cell-1-0-8{
        padding: 0 5px;
    }
    .layui-btn+.layui-btn{
        margin-left: 0;
    }
    .layui-layer-setwin{
        right:25px;
    }
</style>
<div class="layui-card-body">
    <div class="layui-tab">
        <ul class="layui-tab-title">
            <li class="{$Think.get.status==''?'layui-this':''}"><a href="/order/index">全部订单 <span class="green">{$count['count_all']}</span></a></li> 
            <li class="{$Think.get.status==1?'layui-this':''}"><a href="?status=1">待发货 <span class="green">{$count['count_0']}</span></a></li>
            <li class="{$Think.get.status==2?'layui-this':''}"><a href="?status=2">待收货 <span class="green">{$count['count_1']}</span></a></li>
            <li class="{$Think.get.status==10?'layui-this':''}"><a href="?status=10">已完成 <span class="green">{$count['count_2']}</span></a></li>
            <li class="{$Think.get.status=='5,7,8,11'?'layui-this':''}"><a href="?status=5,7,8,11">申请退款 <span class="green">{$count['count_3']}</span></a></li>
            <li class="{$Think.get.status=='4,6,13,14,15'?'layui-this':''}"><a href="?status=4,6,13,14,15">申请售后 <span class="green">{$count['count_4']}</span></a></li>
        </ul>
    </div>
    <!-- todo  选择行打印 -->
    <table class="layui-table" id="dataTable" lay-filter="dataTable">
    </table>

    <script type="text/html" id="expressTpl">
        [{{d.express||'普通快递'}}] {{d.sender_name||''}} {{d.sender_phone||''}}
    </script>
    <script type="text/html" id="addressTpl">
        {{#  if(d.address.length==1){ }}
            {{d.address[0].consignee||''}} [{{d.address[0].phone||''}}]
        {{#  } else { }}
        <table class="layui-table">
            <thead>
                <tr>
                    <th>收货人</th>
                    <th>购买数</th>
                    <th>手机号</th>
                    <th>地址</th>
                    <th>物流公司</th>
                    <th>运单号</th>
                </tr>
            </thead>
            <tbody>
                {{# layui.each(d.address, function(index, item){ }}
                <tr>
                    <td>{{ item.consignee ||''}}</td>
                    <td>{{ item.number ||0}}</td>
                    <td>{{ item.phone ||''}}</td>
                    <td>{{ item.address ||''}}</td>
                    <td>{{ item.express_code ||''}}</td>
                    <td>{{ item.express_number ||''}}</td> 
                </tr>
                {{# }); }}
            </tbody>
        </table>
        {{#  } }} 
    </script>
    <script type="text/html" id="productTpl">
        {{#  if(d.product.length==1){ }}
            {{d.product[0].name||''}} [{{d.product[0].specifications||''}}] 
            {{#  if(d.product[0].postage){ }}
                （邮费￥{{d.product[0].postage }}）
            {{#  } }} 
        {{#  } else { }}
        <table class="layui-table">
            <thead>
                <tr>
                    <th>商品名</th>
                    <th>规格</th>
                    <th>购买数</th>
                    <th>购买价</th>
                </tr>
            </thead>
            <tbody>
                {{# layui.each(d.product, function(index, item){ }}
                <tr>
                    <td>{{ item.name ||''}}</td>
                    <td>{{ item.specifications ||''}}</td>
                    <td>{{ item.number ||0}}</td>
                    <td>￥{{ item.price ||0}}</td> 
                </tr>
                {{# }); }}
            </tbody>
        </table>
        {{#  } }} 
    </script>
    <script type="text/html" id="redpacketTpl">
        {{#  if(!d.red_packet){ }}
        无红包
        {{# } else if(d.red_packet.length==1){ }}
        {{d.red_packet[0].type||''}}  ￥{{ d.red_packet[0].money ||0}} {{d.red_packet[0].user_name||''}}
        {{#  } else { }}
        <table class="layui-table">
            <thead>
                <tr>
                    <th>类型</th>
                    <th>姓名</th>
                    <th>红包</th>
                </tr>
            </thead>
            <tbody>
                {{# layui.each(d.red_packet, function(index, item){ }}
                <tr>
                    <td>{{ item.type ||''}}</td> 
                    <td>{{ item.user_name ||''}}</td> 
                    <td>￥{{ item.money ||0}}</td> 
                </tr>
                {{# }); }}
            </tbody>
        </table>
        {{#  } }} 
    </script>
    <script type="text/html" id="dataTableBar">
        {{#  if(d.status=='待发货'||d.status=='待收货'){ }}
        <a class="layui-btn layui-btn-xs" lay-event="send">发货</a>
        {{#  } else if(d.status=='申请退款'){  }}
        <a class="layui-btn layui-btn-xs" lay-event="send">发货</a>
        <a class="layui-btn layui-btn-xs  layui-btn-normal" lay-event="confirmRefund">确认退款</a> 
        {{#  } else if(d.status=='申请售后'){  }}
        <a class="layui-btn layui-btn-xs  layui-btn-normal" lay-event="confirmRefund">同意退货</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="rejectRefund">拒绝退货</a>
        {{#  } else if(d.status=='卖家待收货'){  }}
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="confirmReceipt">确认收货</a>   
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="reject">驳回</a>     
        {{#  } }}
        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="info">详情</a>
    </script>

    <script type="text/html" id="totalMoney">
        {if condition="($Think.session.admin_info.permission==6)"} 
        {{ d.total_real_supply + d.total_postage ||0}} 
        {elseif condition="$Think.session.admin_info.permission==10"/}
         {{ d.total_money ||0}} 
         {/if}            
    </script>

</div>
<script>
    layui.use(['table', 'http', 'form', 'laydate'], function () {
        var table = layui.table,
            layer = layui.layer,
            http = layui.http,
            $ = layui.$;
        var form = layui.form;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#date',
            range: '~',
            done: function (value, date, endDate) {
                setTimeout(() => {
                    $("#search").click()
                }, 100);
            }
        });

        form.on('submit(search)', function (data) {
            reloadTable(data.field)
            return false;
        });

        form.on('submit(export)', function (data) {
            window.open("/order/export");              
            return false;
        });
        
        table.render({
            elem: '#dataTable',
            url: '/order/index/',
            toolbar: false,
            page: true,
            limit: 20,
            limits: [20, 50, 100, 500],
            cols: [
                [{
                        field: 'id',
                        title: '订单ID',
                        minWidth: 230
                    }, {
                        field: 'ct',
                        title: '下单时间',
                        minWidth: 170
                    }, {
                        templet: '#expressTpl',
                        title: '快递信息',
                        // }, {
                        //     field: 'gang_name',
                        //     title: '帮会',
                    }, {
                        templet: '#addressTpl',
                        title: '收货信息',
                    }, {
                        templet: '#productTpl',
                        title: '商品',
                        minWidth: 300,
                    },

                    {if condition = "($Think.session.admin_info.permission==10)"} {
                        templet: '#redpacketTpl',
                        title: '红包',
                        // }, {
                        //     field: 'buyer_name',
                        //     title: '购买人',
                        // }, {
                        //     field: 'seller_name',
                        //     title: '店主',
                        // }, {
                        //     field: 'inviter_name',
                        //     title: '邀请人',
                    },
                    {/if}             
                    {
                        field: 'status',
                        title: '状态',
                    },
                    {
                        templet: '#totalMoney',
                        title: '总金额',
                    },
                    {
                        field: 'remark',
                        title: '备注',
                    },
                    {
                        title: '操作',
                        toolbar: '#dataTableBar',
                        minWidth: 210,
                        align:'center',
                        // fixed: 'right'
                    }
                ]
            ],
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.message,
                    "count": res.data.count,
                    "data": res.data.data
                };
            },
            where: {
                status: "{$Think.get.status == '' ? -1 : $Think.get.status}"
            }
        });

        
        table.on('tool(dataTable)', function (obj) {
            var id = obj.data.id
            var url = ''
            var title = ''
            switch (obj.event) {
                case 'send':
                    url = '/order/sendGoods?id=' + id
                    title = '发货'
                    break;
                case 'info':
                    url = '/order/orderinfo?id=' + id
                    title = '订单详情'
                    break;
                    
                case 'confirmRefund':
                    http.post('/order/confirmRefund', {
                        order_id: id
                    }, function (res) {
                        reloadTable()
                    })
                    break; 
                case 'rejectRefund':
                    var pindex = layer.prompt({title: '拒绝理由', formType: 2},function(val, index){
                        // layer.msg('得到了'+val);
                        http.post('/order/rejectRefund', {
                            order_id: id,
                            reason:val
                        }, function (res) { 
                            layer.close(pindex);
                            reloadTable()
                        })
                    });
                   
                    break;
                case 'confirmReceipt':
                    http.post('/order/confirmReceipt', {
                            order_id: id, 
                        }, function (res) {                            
                            reloadTable()
                        }) 
                    break;
                case 'reject':
                    var pindex = layer.prompt({title: '驳回理由', formType: 2},function(val, index){
                        // layer.msg('得到了'+val);
                        http.post('/order/reject', {
                            order_id: id,
                            reason:val
                        }, function (res) { 
                            layer.close(pindex);
                            reloadTable()
                        })
                    });
                   
                    break;
                    
            };
            if (url) {
                http.get(url, title);  
            }
        });

        var reloadTable = function (where, sort) {
            table.reload("dataTable", {
                initSort: sort,
                where: where,
            });
        }

        table.on('sort(dataTable)', function (obj) {
            reloadTable({
                field: obj.field,
                order: obj.type
            }, obj)
        });
    });
</script>